package com.helloworld.demo.threadbase;

public class WaitAndSychnized {
    public static void main(String[] args) throws Exception {
        Object lock = new Object();

        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                test(lock);
            }
        });

        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                test(lock);
            }
        });

        t1.start();
        t2.start();

        t1.join();
        t2.join();

    }

    public static void test(Object lock) {
        String name = Thread.currentThread().getName();
        System.out.println(name + "begin...");
        synchronized (lock){
            System.out.println(name + " excute...");
            try {
                //lock.wait(5000);
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(name + "end...");
    }
}
